package Web;

import Bean.Book;
import Bean.Cart;
import Bean.CartItem;
import com.google.gson.Gson;
import service.BookService;
import service.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jianjun
 * @Describe
 * @creat 2021-12-30-22:36
 */
public class CartServlet extends BaseServlet {
    BookService bookService = new BookServiceImpl();

    protected void ajaxAddItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数 商品编号
        int id = Integer.parseInt(request.getParameter("id"));
        //得到id对应得图书对象
        Book book = bookService.queryBookById(id);
        //转化为商品项对象
        CartItem cartItem = new CartItem(id, book.getName(), 1, new BigDecimal(book.getPrice()));
        //注意点：创建购物车对象
        //添加商品项
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            request.getSession().setAttribute("cart", cart);
        }
        //添加商品
        cart.addItem(cartItem);
        request.getSession().setAttribute("book_name", book.getName());

        //返回购物车总的商品数量和最后一个添加的商品名称
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("totalCount",cart.getTotalCount());
        resultMap.put("bookName",book.getName());
        Gson gson = new Gson();
        String json = gson.toJson(resultMap);
        response.getWriter().write(json);
    }

    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Book book = bookService.queryBookById(id);
        CartItem cartItem = new CartItem(id, book.getName(), 1, new BigDecimal(book.getPrice()));
        //注意点：创建购物车对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            request.getSession().setAttribute("cart", cart);
        }
        //添加商品
        cart.addItem(cartItem);
        request.getSession().setAttribute("book_name", book.getName());
        //不能单纯使用重定向。要把当前网页地址也发送过去
        //http协议中请求头有Referer可以获取网页地址的值
        response.sendRedirect(request.getHeader("Referer"));
    }

    //删除商品项
    protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            cart.deleteItem(id);
            response.sendRedirect(request.getHeader("Referer"));
        }
    }

    //清空购物车
    protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
            response.sendRedirect(request.getHeader("Referer"));
        }
    }

    //修改商品数量
    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //商品数量
        int count = Integer.parseInt(request.getParameter("count"));
        //商品编号
        int id = Integer.parseInt(request.getParameter("id"));
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null){
            cart.updateCount(id, count);
            response.sendRedirect(request.getHeader("Referer"));
        }
    }
}
